package mes.kanban.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import mes.kanban.entity.MesConfigList;
import mes.kanban.entity.MesMaterial;
import mes.kanban.exception.MyException;
import mes.kanban.respository.ConfigListRespository;
import mes.kanban.respository.MaterialRespository;
import mes.kanban.service.MaterialServiceI;
import mes.kanban.util.FileUitl;
import mes.kanban.util.IsNotNullValit;
import mes.kanban.util.PageBean;
import mes.kanban.vo.MesMaterialVO;

@Service
public class MaterialServiceImpl implements MaterialServiceI {
	
	private static final Logger log = LoggerFactory.getLogger(MaterialServiceImpl.class);
	
	@Autowired
	private MaterialRespository materialRespository;
	
	@Autowired
	private ConfigListRespository configListRespository;

	@Override
	public List<MesMaterial> getDatas(String userId) {
		
		log.info("MaterialServiceImpl getDatas 查询数据开始");
		
		//List<MesMaterial> jpalist = materialRespository.findAll();
		List<MesMaterial> jpalist = materialRespository.findMaterialListByUserId(userId);
		return jpalist;
		
	}
	
	@Override
	public List<MesMaterialVO> getConfigListByPolingConfigId(String pollingId) {
		
		List<MesConfigList> mcs = configListRespository.getConfigListByPolingConfigId(pollingId);
		
		List<String> ids = new ArrayList<String>();
		
		List<MesMaterialVO> vos = new ArrayList<MesMaterialVO>();
		
		for (MesConfigList mc : mcs) {
			ids.add(mc.getId());
		}
		
		List<MesMaterial> materials = materialRespository.findMateriasById(ids);
		
		if (IsNotNullValit.CollectionIsNotNull(materials)) {
			for (MesMaterial mesMaterial : materials) {
				
				MesMaterialVO vo = new MesMaterialVO();
				
				BeanUtils.copyProperties(mesMaterial, vo);
				
				vos.add(vo);
			}
		}
		
		return vos;
	}
	
	@Override
	public void add(MesMaterial m) {
		
		this.materialRespository.save(m);
		
	}

	@Override
	public MesMaterialVO findMaterialById(String materialId) {
		
		MesMaterial material = materialRespository.findOne(materialId);
		MesMaterialVO vo = new MesMaterialVO();
		BeanUtils.copyProperties(material, vo);
		return vo;
		
	}
	
	@Override
	public PageBean getDatasForPageBean(Integer pageNum,Integer pageSize,String userId) {
		
		PageBean pb = new PageBean();
		
		/*Pageable pa = new PageRequest(pageNum-1, pageSize);
		Page<MesMaterial> pages = this.materialRespository.findAll(pa);
		List<MesMaterial> mats = pages.getContent();
		int allrow = (int) pages.getTotalElements();
		int totalPage = pages.getTotalPages();*/
		List<MesMaterial> list = materialRespository.findMaterialListByUserId(userId);
		List<MesMaterial> mats = this.materialRespository.findMaterialListByPage((pageNum-1)*pageSize,pageSize,userId);
		
		int allrow = list.size();
		int totalPage = (allrow/pageSize) + (allrow%pageSize==0?0:1);
		pb.setList(mats);
		pb.setAllCounts(allrow);
		pb.setTotalPage(totalPage);
		pb.setPageSize(pageSize);
		pb.setCurrentPage(pageNum);
		
		return pb;
	}

	@Override
	public void deleteMaterialById(String id,String userId) {
		
		
		MesMaterialVO material = this.findMaterialById(id);
		
		if (material.getCreator().equals(userId)) {
			
			String saveUrl = material.getUrl()+"/"+material.getMaterialName();
			//删除文件
			FileUitl.deleteFileForWindows(saveUrl);
			
			this.materialRespository.delete(id);
		}else{
			throw new MyException("不是登录用户上传的资源,不能删除");
		}
		
		
	}

	
}
